package com.jservice.api.service.zww;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAppUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Zww0001
 *
 * @Author cwledit
 * @DATE 2017/6/15.
 */
@Service("zww0001")
@Transactional
public class Zww0001 extends BaseService implements IService{

    @Resource(name = "ssoService")
    private SSOService ssoService;

    @Resource
    private Sequence idWorker;

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws Exception {
        Map paramMap =  request.getData();
        String loginname = paramsStringFilter(paramMap.get("loginname"));
        String password = paramsStringFilter(paramMap.get("password"));
        String deviceid = paramsStringFilter(paramMap.get("deviceid"));
        String platform = paramsStringFilter(paramMap.get("platform"));
        if (StringUtils.isEmpty(loginname) || StringUtils.isEmpty(password)){
            return renderFAIL(MsgCode.Code_1008,"用户名或密码为空!",response,header);
        }
        Result result = ssoService.idValidation(loginname, password);//用户认证
        if(result.getResultCode() != 0){
            return renderFAIL(MsgCode.Code_1008,"用户认证失败!",response,header);
        }else{
            String authcode = result.getResultDate().getString("authcode");//授权码
            result = ssoService.getToken(authcode);//根据授权码获取令牌
            if(result.getResultCode() != 0){
                return renderFAIL(MsgCode.Code_1008,"用户认证失败!",response,header);
            }else {
                String accesstoken = (String) result.getResultDate().getString("accesstoken");//访问令牌
                result = ssoService.getUserInfo(accesstoken);//根据访问令牌获取用户详细信息

                if(result.getResultCode() != 0){
                    return renderFAIL(MsgCode.Code_1008,"用户认证失败!",response,header);
                }else{
                    JSONObject userinfo =  result.getResultDate();//用户详细信息
                    String cardId = userinfo.getString("idnum");
                    String authlevel = userinfo.getString("authlevel");
                    //为了测试
//                    cardId = "330322196206083613";
                    if (StringUtils.isEmpty(authlevel) || "1".equals(authlevel) ){
                        Result stResult = ssoService.getST(accesstoken);
                        if (stResult.getResultCode() != 0){
                            return renderFAIL(MsgCode.Code_1008,"用户未实名认证，前往实名认证界面失败，请自行登录【浙江政务网】进行实名认证!",response,header);
                        }else {
                            JSONObject stjson = stResult.getResultDate();
                            String st = stjson.getString("st");
                            response.setData(st);
                            return renderFAIL(MsgCode.Code_1010,"用户还未实名认证!",response,header);
                        }
                    }else{
                        SmkAppUser appUser = SmkAppUser.dao.doFindFirst("id_card = ?",cardId);
                        if (appUser != null){
                            String token = AppUserKit.addAppUser(appUser);
                            appUser.setToken(token);
                            appUser.setPhone(userinfo.getString("mobile"));
                            appUser.setName(userinfo.getString("username"));
                            appUser.setLoginDate(new Date());
                            appUser.setIdCard(cardId);
                            appUser.setLoginType("zww");
                            appUser.setDeviceId(deviceid);
                            appUser.setPlatform(platform);
                            appUser.update();
                        }else{
                            appUser = new SmkAppUser();
                            appUser.setName(userinfo.getString("username"));
                            appUser.setLoginDate(new Date());
                            appUser.setIdCard(cardId);
                            appUser.setPhone(userinfo.getString("mobile"));
                            String token = AppUserKit.addAppUser(appUser);
                            appUser.setToken(token);
                            appUser.setLoginType("zww");
                            appUser.setId(idWorker.nextIdStr());
                            appUser.setDeviceId(deviceid);
                            appUser.setPlatform(platform);
                            appUser.save();
                        }
                        return renderSUC(appUser,response,header);
                    }
                }

            }

        }
    }
}
